<template>
  <div class="page-list-live">
    <!-- 表格 -->
    <ListTable ref="RefListTable" :loading="tableLoading" :table-data="tableData" :table-col="tableCol" />
  </div>
</template>
<script>
import listMixin from '@/mixins/list'
import { parseTime } from '@/utils/index'
import { path } from '@/api/api'
export default {
  mixins: [listMixin],
  props: {
    autoGateList: {
      type: Array,
      default: function() {
        return []
      }
    },
    baseStationList: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      tableData: [],
      listTitle: [
        'employeeId',
        'employee.name',
        'employee.RFID',
        'employee.accessCardID',
        'baseStation.name',
        'baseStation.area.name',
        'autoGate.area.name',
        'autoGate.name',
        'enterTime',
        'leaveTime'
      ],
      tableCol: [
        // { type: 'selection' },
        { type: 'index', label: '序号', width: '60px' },
        {
          label: '员工名称',
          formatter(row, col, cell) {
            let deptName = ''
            if (row.employeeId) {
              deptName = row.employee.name
            }
            return deptName || ''
          },
          minWidth: '80px'
        },
        {
          label: 'RFID',
          formatter(row, col, cell) {
            let rfid = ''
            if (row.employeeId) { rfid = row.employee.rfid }
            return rfid || ''
          }
        },
        {
          label: '门禁ID',
          formatter(row, col, cell) {
            let number = ''
            if (row.employeeId) {
              number = row.employee.accessCardID
            }
            return number || ''
          },
          minWidth: '116px'
        },
        {
          label: '所在区域',
          formatter(row, col, cell) {
            if (row.baseStation && row.baseStation.area) {
              return row.baseStation.area ? row.baseStation.area.name : row.baseStation.name
            } else if (row.autoGate && row.autoGate.area) {
              return row.autoGate.area ? row.autoGate.area.name : row.autoGate.name
            }
          },
          minWidth: '150px'
        },
        {
          label: '设备名称',
          formatter(row, col, cell) {
            if (row.baseStation && row.baseStation.name) {
              return row.baseStation.name
            } else if (row.autoGate) {
              return row.autoGate.name
            }
          },
          minWidth: '250px'
        },
        {
          prop: 'deptCode',
          label: '进入区域时间',
          formatter(row, col, cell) {
            return row.enterTime ? parseTime(new Date(row.enterTime)) : '--'
          },
          minWidth: '160px'
        },
        {
          prop: 'enterTime',
          label: '离开区域时间',
          formatter: function(row, col, cell) {
            return row.leaveTime ? parseTime(new Date(row.leaveTime)) : '--'
          },
          minWidth: '160px'
        }
        // { label: '操作', custom: 'col1' }
      ]
    }
  },
  methods: {
    // 请求列表数据
    async getList(query) {
      this.tableLoading = true
      try {
        let obj = query || { pageIndex: 1, pageSize: 10, orderBy: 'createdTime', orderByAscent: false, search: [] }

        obj = await this.objHandler(obj)
        const {
          data: { items: resData, count, pageIndex }
        } = await path('post', obj, false)
        this.$nextTick(() => {
          this.tableData = []
          resData.forEach(item => {
            item.loadingEdit = false
            item.loadingDel = false
            if (!item.id) {
              item.id = Math.random()
            }
            // if (item.employeeId) {
            //   this.tableData.push(item)
            // }
          })
          this.tableData = JSON.parse(JSON.stringify(resData))
          this.$refs['RefListTable'].update({ page: pageIndex, count })
          this.tableLoading = false
        })
      } catch (err) {
        this.tableLoading = false
      }
    },
    // 处理请求参数
    objHandler(obj) {
      if (!obj.search) {
        obj.search = []
      }
      // 如果选择空数据，需设置值为null
      obj.search.forEach((edit, index) => {
        if (edit.key === 'baseStation.area.name' && edit.value === '空') {
          edit.value = null
        }
      })
      if (!obj.search.some(r => r.key === 'CarId')) {
        obj.search.push({
          key: 'CarId',
          value: null,
          operation: 0,
          relationship: 0,
          sequence: 0
        })
      }
      // 查询设备
      var find = obj.search.find(r => r.key === 'parntd')
      if (find) {
        // 门禁
        const existenceAutoGate = this.autoGateList.find(auto => auto.label === find.value)
        // 基站
        const existencebaseStation = this.baseStationList.find(baseStation => baseStation.label === find.value)
        // 删除
        obj.search.splice(obj.search.indexOf(find), 1)
        // 分别设置
        if (existenceAutoGate && !existencebaseStation) {
          // 门禁
          obj.search.push({
            key: 'autoGateId',
            value: existenceAutoGate.value,
            operation: 0
          })
        } else if (!existenceAutoGate && existencebaseStation) {
          // 基站
          obj.search.push({
            key: 'baseStationId',
            value: existencebaseStation.value,
            operation: 0
          })
        }
      }
      // 查询返回的数据
      obj.select = this.listTitle
      return obj
    }
  }
}
</script>

<style lang="scss" scoped>
  .sc-list-filter-col {
    width: auto;
  }
</style>
